package com.liguang.rcs.admin.service;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.liguang.rcs.admin.db.entity.DodmsRoleEntity;
import com.liguang.rcs.admin.db.repository.DodmsRoleRepository;
import com.liguang.rcs.admin.util.response.PageableBody;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service
@Log
public class DodmsRoleService {

    @Autowired
    private DodmsRoleRepository repository;

    public Set<String> queryByIds(Long[] roleIds) {

        Set<String> roles = Sets.newHashSet();
        if(roleIds.length == 0){
            return roles;
        }
        List<DodmsRoleEntity> entityLst = repository.findByIdInAndEnabledAndDeleted(Arrays.asList(roleIds), true, false);

        if (entityLst == null || entityLst.isEmpty()) {
            return roles;
        }

        for(DodmsRoleEntity role : entityLst){
            roles.add(role.getName());
        }

        return roles;
    }

    public PageableBody<DodmsRoleEntity> querySelective(String name, Integer page, Integer limit, String sort, String order) {

        Sort sort1 = new Sort(Sort.Direction.fromString(order), sort);
        Pageable pageable = PageRequest.of(page - 1, limit, sort1);

        Page<DodmsRoleEntity> rolePage = repository.findAll((Specification<DodmsRoleEntity>) (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> conditionLst = Lists.newArrayList();
            conditionLst.add(criteriaBuilder.equal(root.get("deleted"), false));
            if (!Strings.isNullOrEmpty(name)) {
                conditionLst.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
            }
            return criteriaBuilder.and(conditionLst.toArray(new Predicate[conditionLst.size()]));
        }, pageable);
        return PageableBody.buildFrom(rolePage);
    }

    public DodmsRoleEntity findById(Long id) {
        Optional<DodmsRoleEntity> entity = repository.findById(id);
        return entity.isPresent() ? entity.get() : null;
    }

    public void add(DodmsRoleEntity role) {
        repository.save(role);
    }

    @Transactional
    public void deleteById(Long id) {
        Optional<DodmsRoleEntity> entity = repository.findById(id);
        if (entity.isPresent()) {
            entity.get().setDeleted(true);
            repository.save(entity.get());
        }
    }

    public void updateById(DodmsRoleEntity entity) {
        repository.save(entity);
    }

    public boolean checkExist(String name) {
        List<DodmsRoleEntity> entityList = repository.findByNameAndDeleted(name, false);
        return entityList != null && !entityList.isEmpty();
    }

    public List<DodmsRoleEntity> queryAll() {
        return repository.findByDeleted(false);
    }
}
